#include once "mud-gui/widgets/PushButton.bi"

namespace ui

declare SLOT(pushbutton_pixmap_paint)

constructor PushButton( byval parent as BaseWidget ptr = 0, byref _t_ as string = "Ok" )

	if parent <> 0 then
		if parent->cl <> layout_t.noLayout then
			SetParent(parent)
		else
			ShowError("Cannot insert PushButton into this widget.")
		end if
	end if

	(BaseObject).id = ui.widgetids.pushbutton
	(BaseObject).cl = layout_t.canHaveLayout

	connectSignal(@this,resize,PushButton.resize_handler)
	connectSignal(@this,repaint,PushButton.repaint_handler)
	connectSignal(@this,mousedown,PushButton.mousedown_handler)
	connectSignal(@this,mouseover,PushButton.mouseover_handler)
	connectSignal(@this,destroy,PushButton.destroy_handler)
	connectSignal(@this,clicked,PushButton.click_handler)

	m_label = new label( toBaseWidget(@this), _t_ )
	connectSignal(m_label,repaint,pushbutton_pixmap_paint)
	m_label->setAlignment = ALIGNMENT.center
	setPixmapAlignment = ALIGNMENT.left

end constructor

IMP_PROPERTY( PushButton, pixmap, fb.image ptr )
IMP_PROPERTY( PushButton, pixmapAlignment, ALIGNMENT )

property PushButton.minimumWidth( ) as integer
	return (BaseObject).minwidth
end property

property PushButton.setMinimumWidth( byval _t_ as integer )
	(BaseObject).minwidth = _t_
end property

property PushButton.minimumHeight( ) as integer
	return (BaseObject).minheight
end property

property PushButton.setMinimumHeight( byval _t_ as integer )
	(BaseObject).minheight = _t_
end property

property PushButton.text() as string

	return m_label->text

end property

property PushButton.setText( byref _t_ as string )

	m_label->setText = _t_

end property

STATIC_SLOT(PushButton.click_handler)
	cast( PushButton ptr, ref )->m_isdown = 0
	line ref->buffer, (0,0)-(ref->width,ref->height), rgb(&hc9,&hc9,&hc9 ), BF
	return 0
ENDSLOT

STATIC_SLOT(PushButton.destroy_handler)

	var self = cast( PushButton ptr, ref )

	if self->pixmap <> 0 then imagedestroy self->pixmap

	return FALSE

ENDSLOT

STATIC_SLOT(PushButton.mousedown_handler)

	cast(PushButton ptr, ref)->m_isdown = 1
	return 1

ENDSLOT

STATIC_SLOT(PushButton.mouseover_handler)

	var pb = cast(PushButton ptr, ref)
	if e->m_x <= ref->x+1 OR e->m_y <= ref->y+1 OR e->m_x >= (ref->x + ref->width)-1 OR e->m_y >= (ref->y + ref->width)-1 then
		pb->m_isdown = 0
	end if

	return 1

ENDSLOT

STATIC_SLOT(PushButton.resize_handler)

	ref->buffer = imagecreate( ref->width, ref->height, rgb(&hc9,&hc9,&hc9 ) )
	var lbl = cast(PushButton ptr, ref)->m_label
	lbl->___base.x = ref->x + 2
	lbl->___base.y = ref->y + 2
	lbl->___base.width = ref->width -4
	lbl->___base.height = ref->height -3

	sendEvent(toBaseWidget(lbl),e) 'so we are sure the variables below are valid

	ref->minwidth = lbl->minimumWidth + 4
	ref->minheight = lbl->minimumHeight + 3

	return 0

ENDSLOT

STATIC_SLOT(PushButton.repaint_handler)

	if ref->buffer = 0 then return 0

	line ref->buffer, (0,0)-(ref->width,ref->height), rgb(&hc9,&hc9,&hc9 ), BF

	select case cast(PushButton ptr, ref)->m_isdown
	case 0 'button up
		line ref->buffer, (1,1)-(1,ref->height-1),rgb(255,255,255)
		line ref->buffer, (1,1)-(ref->width-1,1),rgb(255,255,255)
		line ref->buffer, (2,2)-(2,ref->height-2),rgb(255,255,255)
		line ref->buffer, (ref->width-1,1)-(ref->width-1,ref->height-1),rgb(0,0,0)
		line ref->buffer, (ref->width-2,2)-(ref->width-2,ref->height-2),rgb(128,128,128)
		line ref->buffer, (ref->width-1,ref->height-1)-(1,ref->height-1),rgb(0,0,0)
		line ref->buffer, (ref->width-2,ref->height-2)-(2,ref->height-2),rgb(128,128,128)
	case 1 'button down
		line ref->buffer, (1,1)-(1,ref->height-1),rgb(0,0,0)
		line ref->buffer, (1,1)-(ref->width-1,1),rgb(0,0,0)
		line ref->buffer, (2,2)-(2,ref->height-2),rgb(128,128,128)
		line ref->buffer, (ref->width-1,1)-(ref->width-1,ref->height-1),rgb(255,255,255)
		line ref->buffer, (ref->width-2,2)-(ref->width-2,ref->height-2),rgb(255,255,255)
		line ref->buffer, (ref->width-1,ref->height-1)-(1,ref->height-1),rgb(255,255,255)
		line ref->buffer, (ref->width-3,ref->height-2)-(2,ref->height-2),rgb(128,128,128)
	end select



	put (ref->x,ref->y), ref->buffer, PSET

	return 0

ENDSLOT

SLOT(pushbutton_pixmap_paint)

	if ref->parent->id <> widgetids.pushbutton then return 0
	var pb = cast(PushButton ptr,ref->parent)
	var pbr = toBaseWidget(pb)

	if pb->pixmap <> 0 then
		var pbp = pb->pixmap
		dim as integer dx, dy
		var fw = pbp->width
		var fh = pbp->height
		var half_width = int(pbr->width / 2)
		var half_height = int(pbr->height / 2)
		var half_fw = int(fw / 2)
		var half_fh = int(fh / 2)

		select case pb->pixmapAlignment
		case ui.alignment.topleft
			dx = 1 : dy = 1
		case ui.alignment.topcenter
			dx = 1 + (half_width - half_fw) : dy = 1
		case ui.alignment.topright
			dx = (1 + pbr->width) - fw : dy = 1
		case ui.alignment.left
			dx = 1 : dy = 1 + (half_height - half_fh)
		case ui.alignment.right
			dx = (1 + pbr->width) - fw : dy = 1 + (half_height - half_fh)
		case ui.alignment.bottomleft
			dx = 1 : dy = pbr->y + (pbr->height - fh)
		case ui.alignment.bottomcenter
			dx = 1 + (half_width - half_fw) : dy = 1 + (pbr->height - fh)
		case ui.alignment.bottomright
			dx = (1 + pbr->width) - fw : dy = 1 + (pbr->height - fh)
		case else
			dx = 1 + (half_width - half_fw) : dy = 1 + (half_height - half_fh)
		end select

		put (pbr->x+dx,pbr->y+dy), pbp, trans

	end if

	return 0

ENDSLOT

end namespace
